import { ref } from "vue";
import IPC from "../background/ipc";

export default function useTemplateRepositories(linkId, tableId) {
  const typeList = ref([]);

  const templateList = ref([]);

  const templateJson = ref({});

  const templateCode = ref({});

  const getTypelist = async () => {
    //console.log("---getTypelist----");
    let result = await IPC.invoke("template.types", {});
    console.log(result);
    if (result.code < 0) {
      alert(result.msg);
      typeList.value = [];
      return;
    }
    typeList.value = result.data;
  };

  const getTemplateList = async (type) => {
    //console.log("---getTemplateList----");

    let params = {
      linkId: linkId.value,
      tableId: tableId.value,
      type: type,
    };
    console.log(params);
    let result = await IPC.invoke("template.templates", params);
    if (result.code < 0) {
      alert(result.msg);
      templateList.value = [];
      return;
    }
    templateList.value = result.data;
  };

  const getTemplateJson = async (type) => {
    //console.log("---getTemplateJson----");
    let params = {
      linkId: linkId.value,
      tableId: tableId.value,
      type: type,
    };
    console.log(params);
    let result = await IPC.invoke("template.json", params);
    if (result.code < 0) {
      alert(result.msg);
      templateJson.value = {};
      return;
    }
    templateJson.value = result.data;
  };

  const getTemplateCode = async (type, templateId) => {
    //console.log("---getTemplateCode----");
    let params = {
      linkId: linkId.value,
      tableId: tableId.value,
      type: type,
      templateId: templateId,
    };
    console.log(params);
    let result = await IPC.invoke("template.code", params);
    if (result.code < 0) {
      alert(result.msg);
      templateCode.value = {};
      return;
    }
    templateCode.value = result.data;
  };

  return {
    typeList,
    templateList,
    templateJson,
    templateCode,
    getTypelist,
    getTemplateList,
    getTemplateJson,
    getTemplateCode,
  };
}
